package new

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

const (
	defaultPkgName = "admin"
	VueAddr        = "https://gitee.com/feizhao/admin-vue.git"
	ReactAddr      = "https://gitee.com/feizhao/admin-react.git"
	GoGinAddr      = "https://gitee.com/feizhao/admin-gin.git"
	PhpTpAddr      = "https://gitee.com/feizhao/admin-tp5.git"
	PhpYiiAddr     = "https://gitee.com/feizhao/admin-yii2.git"
)

// Config 配置参数
type Config struct {
	Dir             string
	PkgName         string
	IncludeGoGin    bool
	IncludePhpTp    bool
	IncludePhpYii   bool
	IncludeWebVue   bool
	IncludeWebReact bool
}

// Exec 执行创建项目命令
func Exec(cfg Config) error {
	cmd := &Command{cfg: &cfg}
	return cmd.Exec()
}

// Command 创建项目命令
type Command struct {
	cfg *Config
}

// Exec 执行命令
func (a *Command) Exec() error {
	dir, err := filepath.Abs(a.cfg.Dir)
	if err != nil {
		return err
	}

	log.Printf("项目生成目录：%s", dir)

	notExist := false
	_, err = os.Stat(dir)
	if err != nil {
		if os.IsNotExist(err) {
			notExist = true
		} else {
			return err
		}
	}
	if notExist {
		log.Printf("目录存在,%s", dir)
		return nil
	}

	//gin 框架
	if a.cfg.IncludeGoGin {
		source := GoGinAddr
		err = a.gitClone(filepath.Join(dir, "admin-gin"), source)
	}

	if a.cfg.IncludePhpTp {
		source := PhpTpAddr
		err = a.gitClone(filepath.Join(dir, "admin-tp"), source)
	}

	if a.cfg.IncludePhpYii {
		source := PhpYiiAddr
		err = a.gitClone(filepath.Join(dir, "admin-yii"), source)
	}

	if a.cfg.IncludeWebVue {
		source := VueAddr
		err = a.gitClone(filepath.Join(dir, "admin-vue"), source)
	}

	if a.cfg.IncludeWebReact {
		source := ReactAddr
		err = a.gitClone(filepath.Join(dir, "admin-react"), source)
	}

	// if pkgName := a.cfg.PkgName; pkgName != "" {
	// 	err := a.changePkgName(dir, a.cfg.PkgName)
	// 	if err != nil {
	// 		return err
	// 	}

	// 	err = a.readAndReplaceFile(a.cfg.PkgName, fmt.Sprintf("%s/%s", dir, "go.mod"))
	// 	if err != nil {
	// 		return err
	// 	}
	// }

	// if notExist {
	// 	err = a.gitInit(dir)
	// 	if err != nil {
	// 		return err
	// 	}
	// }

	fmt.Printf("\n项目创建成功：%s\n", dir)
	fmt.Println(TplProjectStructure)

	return nil
}

func (a *Command) execGit(dir string, args ...string) error {
	cmd := exec.Command("git", args...)
	if dir != "" {
		cmd.Dir = dir
	}
	return cmd.Run()
}

func (a *Command) gitClone(dir, source string) error {
	var args []string
	args = append(args, "clone")
	args = append(args, "-q")

	branch := "master"

	args = append(args, "-b", branch)

	args = append(args, source)
	args = append(args, dir)

	log.Printf("执行命令：git %s", strings.Join(args, " "))
	return a.execGit("", args...)
}

func (a *Command) gitInit(dir string) error {
	os.RemoveAll(filepath.Join(dir, ".git"))
	if a.cfg.IncludeGoGin {
		os.RemoveAll(filepath.Join(dir, "admin-gin", ".git"))
	}
	if a.cfg.IncludeWebVue {
		os.RemoveAll(filepath.Join(dir, "admin-vue", ".git"))
	}

	if a.cfg.IncludePhpTp {
		os.RemoveAll(filepath.Join(dir, "admin-tp5", ".git"))
	}
	if a.cfg.IncludeWebVue {
		os.RemoveAll(filepath.Join(dir, "admin-vue", ".git"))
	}
	if a.cfg.IncludePhpYii {
		os.RemoveAll(filepath.Join(dir, "admin-yii2", ".git"))
	}
	return nil
}
